import React, { Component } from 'react';
import { ImageBackground, TextInput, View, Image, StatusBar, Text, TouchableOpacity, FlatList, RefreshControl, DeviceEventEmitter } from 'react-native';
import TabBarTitle from '../components/TabBarTitle';
import TabBarImage from '../components/TabBarImage';
import Colors from '../constants/Colors';
import { statusBarHeight, titleHeight, width, unitWidth } from '../constants/AdapterUtil';
import ScrollableTabView, { ScrollableTabBar, DefaultTabBar } from 'react-native-scrollable-tab-view';
import { LeftComponent } from '../components/MyHeader';
import { SubmitButton6, SubmitButton7 } from '../components/Button';
import MyListView from '../components/MyListView';
import { DeleteTextView } from '../components/MyTextView';
import { Rating, AirbnbRating } from 'react-native-elements';
import NavigationService from '../constants/NavigationService';
import { postFetch } from '../constants/HttpExtension';
import { api_order_workorder_workorderList } from '../constants/HttpServices';

export default class SearchOrderScreen extends Component {

    constructor(props) {
        super(props)
        this.state = {
            searchText: '',
            list: [],
        }
    }

    componentDidMount() {
        this.props.navigation.addListener('didFocus', payload => {
            this.refs.MyListView.onRefresh();
        })
    }

    render() {
        return (
            <View style={{ flex: 1, backgroundColor: Colors.white }}>
                <View style={{ height: statusBarHeight + titleHeight, width: width, backgroundColor: Colors.white }}>
                    <View style={{ marginTop: statusBarHeight, height: titleHeight, width: width, backgroundColor: Colors.white, flexDirection: 'row', borderWidth: 0.5, borderBottomColor: Colors.colorE8, borderColor: Colors.transparent }}>
                        <View style={{ flex: 1, justifyContent: 'center' }}>
                            <LeftComponent leftClick={() => { this.props.navigation.pop(); }} leftImg={require('../res/ic_arrow_left.png')} />
                        </View>
                        <View style={{ flex: 5, alignItems: 'center', justifyContent: 'center' }}>
                            <View style={{ width: 250 * unitWidth, height: 29 * unitWidth, borderRadius: 20, backgroundColor: Colors.colorEF, flexDirection: 'row', alignItems: 'center', }}>
                                <Image source={require('../res/ic_search.png')}
                                    style={{ width: 13 * unitWidth, height: 14 * unitWidth, marginHorizontal: 10 * unitWidth }}
                                    resizeMode='stretch' />
                                <TextInput
                                    style={{ fontSize: 13, height: 35 * unitWidth, width: 250 * unitWidth }}
                                    onChangeText={this.onSearchTextChange.bind(this)}
                                    maxLength={20}
                                    placeholder='搜索'
                                    value={this.state.searchText}
                                />
                            </View>
                        </View>
                        <View style={{ flex: 1, justifyContent: 'center', alignItems: 'flex-end' }}>
                            <SubmitButton6 text='搜索' onPress={() => { this.doSearch() }} />
                        </View>
                    </View>
                </View>
                <MyListView
                    ref='MyListView'
                    list={this.state.list}
                    renderItem={this.getItem.bind(this)}
                    getList={this.getData.bind(this)}
                    canLoadMore={true}
                />
            </View >
        )
    }

    onSearchTextChange(text) {
        this.setState({
            searchText: text
        }, () => {

        })
    }

    doSearch() {
        this.refs.MyListView.onRefresh();
    }

    getItem({ item, index }) {
        return (
            <View style={{ justifyContent: 'center', alignItems: 'center', width: width, height: 161 * unitWidth }}>
                <TouchableOpacity style={{
                    width: width,
                    height: 161 * unitWidth,
                    backgroundColor: Colors.white, justifyContent: 'center'
                }} activeOpacity={0.7}
                    onPress={() => {
                        this.props.navigation.navigate('DemandDetail', { id: item.id })
                    }}>
                    <View style={{
                        paddingHorizontal: 20 * unitWidth,
                        flexDirection: 'row', alignItems: 'center'
                    }}>
                        <Image style={{ width: 115 * unitWidth, height: 90 * unitWidth }} source={{ uri: item.picture }} resizeMode='cover' />
                        <View style={{ alignItems: 'flex-start', marginLeft: 7 * unitWidth }}>
                            <Text style={{ color: Colors.black, fontSize: 15 }}>{item.title}</Text>
                            <Text style={{ color: Colors.color99, fontSize: 13, marginTop: 7 * unitWidth }}>加工数目：{item.total_count}</Text>
                            <View style={{ flexDirection: 'row', alignItems: 'center', marginTop: 7 * unitWidth }}>
                                <Image style={{ width: 13 * unitWidth, height: 16 * unitWidth }} source={require('../res/syss_dddw.png')} resizeMode='stretch' />
                                <Text style={{ color: Colors.color99, fontSize: 13, marginLeft: 5 * unitWidth }}>地区：{item.province + '   ' + item.city}</Text>
                            </View>
                            <View style={{ flexDirection: 'row', alignItems: 'center', marginTop: 7 * unitWidth }}>
                                <Image style={{ width: 14 * unitWidth, height: 14 * unitWidth }} source={require('../res/fws_clsj.png')} resizeMode='stretch' />
                                <Text style={{ color: Colors.color99, fontSize: 13, marginLeft: 5 * unitWidth }}>截止报价日期：</Text>
                                <Text style={{ color: Colors.colorOrange, fontSize: 13, marginLeft: 5 * unitWidth }}>{item.end_date}</Text>
                            </View>
                        </View>
                        <View style={{ alignSelf: 'flex-start', alignItems: 'center', position: 'absolute', right: 15 * unitWidth }}>
                            <Text style={{ color: Colors.color66, fontSize: 13, marginTop: 10 * unitWidth }}>{item.createtime}发布</Text>
                        </View>
                    </View>
                    <View style={{ flexDirection: 'row', alignItems: 'center', marginTop: 15 * unitWidth, justifyContent: 'space-between', marginHorizontal: 15 * unitWidth }}>
                        <Text style={{ color: Colors.color99, fontSize: 13, marginLeft: 5 * unitWidth }}>
                            已报价服务商
                                            <Text style={{ color: Colors.colorOrange }}>   {item.quoted_price_count}家</Text>
                        </Text>
                        <SubmitButton7 style={{ marginLeft: 5 * unitWidth }} text='立即报价' onPress={() => {
                            this.props.navigation.navigate('Quote', { id: item.id })
                        }} />
                    </View>
                    {index == this.state.list.length - 1 ?
                        null : <View style={{ backgroundColor: Colors.colorE8, width: 338 * unitWidth, alignSelf: 'center', height: 0.5, marginTop: 10 * unitWidth, position: 'absolute', bottom: 0 }} />
                    }
                </TouchableOpacity>
            </View>
        )
    }

    getData(page) {
        let params = {
            page: page,
        }
        if (this.state.searchText && this.state.searchText != '') {
            params['title'] = this.state.searchText
        }
        postFetch(api_order_workorder_workorderList, params, {
            success: (response) => {
                if (page == 1) {
                    this.setState({
                        list: response.result.list,
                    }, () => {
                        this.refs.MyListView.onHasMore();
                    })
                } else {
                    if (response.result.list && response.result.list.length > 0) {
                        this.setState({
                            list: this.state.list.concat(response.result.list),
                        }, () => {
                            this.refs.MyListView.onHasMore();
                        })
                    } else {
                        this.refs.MyListView.onNoMore();
                    }
                }
            },
            fail: (response) => {
                show(response.result.message);
            }
        })
    }
}